import 'package:flutter/material.dart';
import 'package:flutterclue/mvp/mode/ClueDetailBean.dart';
import 'package:flutterclue/mvp/mode/ClueEntryDataListBean.dart';
import 'package:flutterclue/mvp/presenter/ClueDetailPagePresenter.dart';
import 'package:flutterclue/utils/SharePreferensUtil.dart';
import 'package:flutterclue/utils/ShowSnackBarUtil.dart';
import 'package:flutterclue/utils/ToastUtilForAndroid.dart';
import 'package:flutterclue/view/progreess_dialog.dart';
import 'package:flutterclue/view/EndLine.dart';

///线索详情
class ClueDetailPage extends StatefulWidget {
  int mClueId;

  ClueDetailPage({int clueId}) : mClueId = clueId;

  @override
  _ClueDetailPageState createState() => new _ClueDetailPageState();
}

final GlobalKey<ScaffoldState> scaffoldStateKey =
    new GlobalKey<ScaffoldState>();

class _ClueDetailPageState extends State<ClueDetailPage>
    implements ClueDetailPageView {
  ClueDetailBean _clueDetailBean;
  Map _followStateMap = {
    1: "跟进中",
    2: "上门接车",
    3: "车辆到店",
    4: "流失",
    5: "已发短信",
    6: "已拨电话",
    7: "店内接待",
    8: "已定损",
    9: "已交车",
    11: "创建线索"
  };

  ClueDetailPagePresenter _clueDetailPagePresenter;
  List<EntryBeanData> _dataList = new List();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    SharePreferencesUtil.saveString("clueIdJ", "");
    SharePreferencesUtil.saveString("messageTypeJ", "");
    _clueDetailPagePresenter = new ClueDetailPagePresenter();
    _clueDetailPagePresenter.attachView(this);
    getData();
  }

  getData() async {
    _clueDetailPagePresenter.getClueDetailData(widget.mClueId);
    _clueDetailPagePresenter.getEntryDataList(widget.mClueId);
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      key: scaffoldStateKey,
      appBar: new AppBar(
        title: new Text(
          "线索详情",
          style: new TextStyle(color: Colors.white),
        ),
        actions: <Widget>[
          new IconButton(
              color: Colors.white,
              icon: new Icon(Icons.colorize),
              onPressed: () {
                scaffoldStateKey.currentState.showSnackBar(new SnackBar(
                  content: new Text("点击了编辑"),
                  duration: new Duration(seconds: 1),
                ));
              }),
          new IconButton(
              icon: new Icon(Icons.delete),
              color: Colors.white,
              onPressed: () {
                scaffoldStateKey.currentState.showSnackBar(new SnackBar(
                  content: new Text("点击了删除"),
                  duration: new Duration(seconds: 1),
                ));
              }),
        ],
        centerTitle: true,
      ),
      body: new Column(
        children: <Widget>[
          Expanded(
            child: new ListView.builder(
                itemBuilder: (context, index) {
                  return buildClueItem(context, index);
                },
                itemCount: _dataList.length + 1,
                physics: new AlwaysScrollableScrollPhysics()),
          ),
          new Divider(
            height: 1,
          ),
          Container(
            color: Colors.white,
            child: Padding(
              padding: EdgeInsets.only(right: 16, top: 5, bottom: 5),
              child: new Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  SizedBox(
                    width: 60,
                    child: new RaisedButton(
                      elevation: 0.0,
                      color: Colors.white,
                      onPressed: () {},
                      child: new Column(
                        children: <Widget>[
                          new Icon(Icons.phone, color: Color(0xff666666)),
                          new Text("电话",
                              style: TextStyle(
                                  color: Color(0xff666666), fontSize: 10))
                        ],
                      ),
                    ),
                  ),
                  SizedBox(
                    width: 60,
                    child: new RaisedButton(
                      elevation: 0.0,
                      color: Colors.white,
                      onPressed: () {},
                      child: new Column(
                        children: <Widget>[
                          new Icon(Icons.sms, color: Color(0xff666666)),
                          new Text("短信",
                              style: TextStyle(
                                  color: Color(0xff666666), fontSize: 10))
                        ],
                      ),
                    ),
                  ),
                  new Expanded(
                      child: new RaisedButton(
                    onPressed: () {},
                    elevation: 0.0,
                    child: Center(
                        child: new Text(
                      "录入跟进",
                      style: new TextStyle(color: Colors.white),
                    )),
                    color: Theme.of(context).accentColor,
                  ))
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  @override
  dismissLoadingView() {}

  @override
  void onFail(String s) {
    ToastUtil.showShort(s);
  }

  @override
  void onSuccessData(ClueDetailBean clueDetailBean) {
    if (!mounted) {
      return;
    }
    _clueDetailBean = clueDetailBean;
    setState(() {});
  }

  @override
  showLoadingView() {}

  Widget buildClueItem(BuildContext context, int index) {
    if (index == 0) {
      if (_clueDetailBean == null) {
        return getProgressDialog();
      } else {
        return buildNo1Item(context);
      }
    } else {
      if (_dataList.length > 0) {
        if (index == 1) {
          if (_dataList.length == 1) {
            return buildItem(context, false, index - 1, true);
          } else {
            return buildItem(context, false, index - 1, false);
          }
        } else if (index < _dataList.length) {
          return buildItem(context, true, index - 1, false);
        } else if (index == _dataList.length) {
          return buildItem(context, true, index - 1, true);
        }
      }
    }
  }

  Widget buildItem(BuildContext context, bool isShow, int index, bool isShow1) {
    return Container(
      color: Colors.white,
      child: Column(
        children: <Widget>[
          new Offstage(
            offstage: isShow,
            child: SizedBox(
              height: 40,
              child: Padding(
                padding: EdgeInsets.only(left: 15),
                child: Row(
                  children: <Widget>[
                    Icon(Icons.attach_file,
                        size: 16, color: Theme.of(context).accentColor),
                    Padding(
                      padding: EdgeInsets.only(left: 12),
                      child: Text("跟进记录",
                          style: TextStyle(color: Color(0xff333333))),
                    )
                  ],
                ),
              ),
            ),
          ),
          Offstage(child: new Divider(height: 1), offstage: isShow),
          Offstage(
              child: new Padding(padding: new EdgeInsets.only(top: 10)),
              offstage: isShow),
          new Container(
            color: Colors.white,
            padding: new EdgeInsets.only(left: 15, right: 15),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                new Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: <Widget>[
                    Text(_dataList[index].CreateTime.substring(5, 10),
                        style: new TextStyle(
                            color: Color(0xff333333), fontSize: 16)),
                    Text(_dataList[index].CreateTime.substring(11, 16),
                        style: new TextStyle(
                            color: Color(0xff666666), fontSize: 14))
                  ],
                ),
                new Padding(
                  padding: EdgeInsets.only(left: 8),
                  child: new Column(
                    children: <Widget>[
                      new Offstage(
                        offstage: index == 0 ? false : true,
                        child: new Container(
                          height: 15,
                          width: 15,
                          decoration: new BoxDecoration(
                              color: Theme.of(context).accentColor,
                              shape: BoxShape.circle,
                              boxShadow: <BoxShadow>[
                                new BoxShadow(
                                  color: Colors.blueAccent,
                                  blurRadius: 4,
                                )
                              ]),
                        ),
                      ),
                      new Offstage(
                        offstage: index == 0 ? true : false,
                        child: new Container(
                            height: 12,
                            width: 12,
                            decoration: new BoxDecoration(
                                color: Color(0xffcccccc),
                                shape: BoxShape.circle)),
                      ),
                      new Offstage(
                        offstage: isShow1,
                        child: new Container(
                          width: 1,
                          color: Color(0xff999999),
                          height: 100,
                        ),
                      )
                    ],
                  ),
                ),
                new Padding(
                  padding: EdgeInsets.only(left: 15),
                  child: new Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(_followStateMap[_dataList[index].State],
                          style: new TextStyle(
                              color: Color(0xff999999), fontSize: 16)),
                      Offstage(
                        offstage: false,
                        child: Padding(
                          padding: EdgeInsets.only(top: 12),
                          child: Text(
                              "下次跟进时间:${_dataList[index].NextFollowUpTime}",
                              style: new TextStyle(
                                  color: Color(0xff999999), fontSize: 14)),
                        ),
                      ),
                      Offstage(
                        offstage: false,
                        child: Padding(
                          padding: EdgeInsets.only(top: 12, bottom: 10),
                          child: Text("备注${_dataList[index].Remark}",
                              style: new TextStyle(
                                  color: Color(0xff999999), fontSize: 14)),
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  child: new Align(
                    alignment: Alignment.topRight,
                    child: new Text(
                      _dataList[index].AgentName,
                      style: new TextStyle(color: Color(0xff999999)),
                    ),
                  ),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget buildNo1Item(BuildContext context) {
    return Container(
      color: Color(0xfff4f4f4),
      child: Column(
        children: <Widget>[
          new Container(
            color: Colors.white,
            padding: new EdgeInsets.only(left: 16, right: 16, top: 16),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                new Row(
                  children: <Widget>[
                    Icon(Icons.card_giftcard,
                        size: 12, color: Color(0xff999999)),
                    Expanded(
                        child: new Padding(
                      padding: EdgeInsets.only(left: 5),
                      child: new Text("保险公司线索",
                          style: TextStyle(
                              color: Color(0xff999999), fontSize: 12)),
                    )),
                    Text(
                        _getDateFormat(_clueDetailBean.UpdateTime,
                            _clueDetailBean.TimeDifference),
                        style:
                            TextStyle(color: Color(0xff999999), fontSize: 12))
                  ],
                ),
                new Padding(padding: EdgeInsets.only(top: 10)),
                new Row(
                  children: <Widget>[
                    Text(_clueDetailBean.LicenseNo,
                        style: TextStyle(
                            color: Color(0xff333333),
                            fontSize: 18,
                            fontWeight: FontWeight.bold)),
                    Padding(
                      padding: EdgeInsets.only(left: 4),
                      child: Text("（超时${_clueDetailBean.TimeoutNoticeCount}次）",
                          style: TextStyle(
                              color: Color(0xffFF4444), fontSize: 14)),
                    )
                  ],
                ),
                new Padding(padding: EdgeInsets.only(top: 10)),
                new Container(
                  padding: EdgeInsets.all(6),
                  color: Color(0xffF2F4F5),
                  child: new Text(_clueDetailBean.SmsContent,
                      style: TextStyle(color: Color(0xff666666), fontSize: 14)),
                ),
                Offstage(
                  offstage: false,
                  child: new Column(
                    children: <Widget>[
                      new Padding(padding: EdgeInsets.only(top: 8)),
                      new Row(
                        children: <Widget>[
                          Text("手机号",
                              style: TextStyle(
                                  color: Color(0xff999999), fontSize: 14)),
                          Padding(
                            padding: EdgeInsets.only(left: 20),
                            child: Text(_clueDetailBean.Mobile,
                                style: TextStyle(
                                    color: Color(0xff666666), fontSize: 14)),
                          )
                        ],
                      ),
                    ],
                  ),
                ),
                new Padding(padding: EdgeInsets.only(top: 8)),
                new Row(
                  children: <Widget>[
                    Text("报案人",
                        style:
                            TextStyle(color: Color(0xff999999), fontSize: 14)),
                    Padding(
                      padding: EdgeInsets.only(left: 20),
                      child: Text(_clueDetailBean.ReportCasePeople,
                          style: TextStyle(
                              color: Color(0xff666666), fontSize: 14)),
                    )
                  ],
                ),
                new Padding(padding: EdgeInsets.only(top: 8)),
                Container(
                  child: new Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text("承保险种",
                          style: TextStyle(
                              color: Color(0xff999999), fontSize: 14)),
                      Expanded(
                        child: Padding(
                          padding: EdgeInsets.only(left: 4),
                          child: Text(_clueDetailBean.InsureInfo,
                              textDirection: TextDirection.ltr,
                              style: TextStyle(
                                  color: Color(0xff666666), fontSize: 14)),
                        ),
                      )
                    ],
                  ),
                ),
                new RaisedButton(
                    padding: EdgeInsets.only(left: 70),
                    color: Colors.white,
                    elevation: 0.0,
                    onPressed: () {
                      scaffoldStateKey.currentState.showSnackBar(new SnackBar(
                        content: new Text("去查询保单"),
                        duration: new Duration(seconds: 1),
                      ));
                    },
                    child: new Text(
                      "保单信息",
                      style: new TextStyle(
                          fontSize: 14, color: Theme.of(context).accentColor),
                    ))
              ],
            ),
          ),
          new Padding(padding: EdgeInsets.only(top: 10))
        ],
      ),
    );
  }

  String _getDateFormat(String createTime, double timeDifference) {
    if (timeDifference == null) {
      return createTime;
    }
    double mMessageTime = timeDifference / 60 + 0.5;
    if (mMessageTime < 60) {
      if (mMessageTime == 0) {
        return "现在";
      } else {
        return "${mMessageTime}分钟前";
      }
    } else if (mMessageTime < 1439 && mMessageTime >= 60) {
      return "${(mMessageTime / 60.0 + 0.5).toInt()}小时前";
    } else {
      return createTime.substring(0, 10);
    }
  }

  @override
  void onSuccessListEntry(EntryListBean entryListBean) {
    if (!mounted) {
      return;
    }
    _dataList.clear();
    entryListBean.list.forEach((e) {
      e.Data.forEach((f) {
        _dataList.add(f);
      });
    });
    setState(() {});
  }
}
